package edu.task.service.impl;

import edu.task.mapper.Mapper;
import edu.task.model.SysExplain;
import edu.task.model.SysSubtask;
import edu.task.model.SysTask;
import edu.task.model.SysTaskSubtask;
import edu.task.model.vo.SysSubtaskVO;
import edu.task.repository.SysSubtaskRepository;
import edu.task.repository.SysTaskSubtaskRepository;
import edu.task.service.SysSubtaskService;
import edu.task.utils.BeanUtils;
import edu.task.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Service
public class SysSubtaskServiceImpl implements SysSubtaskService {
    @Autowired
    SysSubtaskRepository sysSubtaskRepository;

    @Autowired
    Mapper mapper;

    @Autowired
    SysTaskSubtaskRepository sysTaskSubtaskRepository;


    @Override
    public List<SysSubtask> selectSubtaskList() {
        return sysSubtaskRepository.findAll();
    }

    @Override
    public boolean insertSubtask(SysSubtaskVO subtask) {
        SysSubtask sysSubtask = new SysSubtask();
        BeanUtils.copyProperties(subtask, sysSubtask);
        sysSubtask.setStatus(0);
        mapper.insertSubtask(sysSubtask);
        subtask.setSubtaskId(sysSubtask.getSubtaskId());
        insertTaskSubtask(subtask);
        return null != sysSubtask.getSubtaskId();
    }

    @Override
    @Transactional
    public int deleteSubtaskById(Long subtaskId) {
        sysSubtaskRepository.deleteTaskSubtaskBySubtaskId(subtaskId);
        return sysSubtaskRepository.deleteSubtaskById(subtaskId);
    }

    @Override
    @Transactional
    public boolean updateSubtask(SysSubtaskVO subtask) {
        Long subtaskId = subtask.getSubtaskId();
        Optional<SysSubtask> op = sysSubtaskRepository.findById(subtaskId);
        if (!op.isPresent()) {
            return false;
        }
        // 删除任务与子任务关联
        sysSubtaskRepository.deleteTaskSubtaskBySubtaskId(subtaskId);
        SysSubtask sysSubtask = op.get();
        BeanUtils.copyPropertiesIgnoreNull(subtask, sysSubtask);
        //更新子任务信息
        sysSubtaskRepository.save(sysSubtask);
        //新增任务和子任务关联
        insertTaskSubtask(subtask);
        return null != sysSubtask.getSubtaskId();
    }

    @Override
    public List<SysSubtask> selectSubtaskListByTaskId(Long taskId) {
        return sysSubtaskRepository.selectSubtaskListByTaskId(taskId);
    }

    @Transactional
    public int  insertTaskSubtask(SysSubtaskVO subtask) {
        Long[] tasks = subtask.getTaskIds();
        if (StringUtils.isNull(tasks) || tasks.length == 0 ) {
            return 0;
        }
        Long[] subtaskIds = new Long[tasks.length];
        Long subtaskId = subtask.getSubtaskId();
        for (Long taskId : tasks) {
            SysTaskSubtask sysTaskSubtask = new SysTaskSubtask();
            sysTaskSubtask.setTaskId(taskId);
            sysTaskSubtask.setSubtaskId(subtaskId);
            sysTaskSubtaskRepository.save(sysTaskSubtask);
        }
        Arrays.fill(subtaskIds, subtask.getSubtaskId());
        return 1;
    }
}
